package com.example.admin.service.impl;

import com.example.admin.model.User;
import com.example.admin.repository.UserRepository;
import com.example.admin.service.UserService;
import jakarta.persistence.criteria.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    @Override
    public List<User> findByName(String userName) {
        return userRepository.findByName(userName);
    }

    @Override
    public User saveUser(User user) {
        return userRepository.save(user);
    }


    @Override
    public List<Object> getBasicList() {
        return userRepository.getBasicList();
    }

    @Override
    public Page<User> getList(String firstName, String phone, int age, int page, int size) {
        // 构建动态查询条件
        Specification<User> spec = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (firstName != null && !firstName.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("name"), firstName));
            }

            if (phone != null && !phone.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("phone"), "%" + phone + "%"));
            }
            // 可以根据需要添加更多条件
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        // 执行分页查询
        PageRequest pageRequest = PageRequest.of(page, size);
        return userRepository.findAll(spec, pageRequest);
    }

    @Override
    public boolean updateUser(Long id, User updateUser) {
        Optional<User> optionalUser = userRepository.findById(id);
        if (optionalUser.isPresent()) {
            User existingUser = optionalUser.get();
            // 更新传入的字段值，如果字段为空则不更新
            if (updateUser.getName() != null) {
                existingUser.setName(updateUser.getName());
            }
            if (updateUser.getPhone() != null) {
                existingUser.setPhone(updateUser.getPhone());
            }
            if (updateUser.getAvatar() != null) {
                existingUser.setAvatar(updateUser.getAvatar());
            }
            if (updateUser.getBase_company_id() != null) {
                existingUser.setBase_company_id(updateUser.getBase_company_id());
            }
            if (updateUser.getBase_department_id() != null) {
                existingUser.setBase_department_id(updateUser.getBase_department_id());
            }
            if (updateUser.getUsername() != null) {
                existingUser.setUsername(updateUser.getUsername());
            }
            if (updateUser.getPassword() != null) {
                existingUser.setPassword(updateUser.getPassword());
            }

            userRepository.save(existingUser);
            return true;
        } else {
            return false;  // 用户不存在
        }
    }

    @Override
    public boolean deleteUser(Long id) {
        userRepository.deleteById(id);
        return true;
    }
}
